package org.erp.procurement.config;

import lombok.AllArgsConstructor;
import org.erp.procurement.enums.OrderEventEnum;
import org.erp.procurement.enums.OrderStatusEnum;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.EnumSet;

/**
 * 状态机配置
 *
 * @author libin
 * @version 1.0
 * @description: 状态机配置
 * @date 2025/11/5 11:27
 **/
@Configuration
@AllArgsConstructor
@EnableStateMachineFactory
public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<OrderStatusEnum, OrderEventEnum> {
    @Override
    public void configure(StateMachineStateConfigurer<OrderStatusEnum, OrderEventEnum> states) throws Exception {
        states.withStates()
                .initial(OrderStatusEnum.TO_BE_SHIPPED)
                .states(EnumSet.allOf(OrderStatusEnum.class))
                .end(OrderStatusEnum.DONE)
                .end(OrderStatusEnum.CANCELLED);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<OrderStatusEnum, OrderEventEnum> transitions) throws Exception {
        transitions
                .withExternal().source(OrderStatusEnum.TO_BE_SHIPPED).target(OrderStatusEnum.QUALITY_INSPECTION).event(OrderEventEnum.CONFIRM_RECEIPT).and()
                .withExternal().source(OrderStatusEnum.QUALITY_INSPECTION).target(OrderStatusEnum.TO_BE_CONFIRMED).event(OrderEventEnum.CONFIRM_QUOTE).and()
                .withExternal().source(OrderStatusEnum.TO_BE_CONFIRMED).target(OrderStatusEnum.DONE).event(OrderEventEnum.FINISH).and()
                .withExternal().source(OrderStatusEnum.TO_BE_CONFIRMED).target(OrderStatusEnum.CANCELLED).event(OrderEventEnum.CANCEL);
    }
}
